﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wormhole.Engine.Communication;

namespace Wormhole.Test.Communication
{
	class Program
	{
		#region Declarations
		/// <summary>
		/// The communications client.
		/// </summary>
		protected static IClient client = null;
		#endregion

		static void Main(string[] args)
		{
			try
			{
				// create the client
				client = new IrcClient("localhost", 6667, "#lobby", "Jack");
				client.RandomSeedReceived += new RandomSeedReceivedDelegate(client_RandomSeedReceived);
				client.OpenGameReceived += new OpenGameReceivedDelegate(client_OpenGameReceived);
				client.DeckReceived += new DeckReceivedDelegate(client_DeckReceived);
				client.JoinRequest += new JoinRequestDelegate(client_JoinRequest);
				client.JoinResponse += new JoinResponseDelegate(client_JoinResponse);

				client.RequestOpenGameList();
				client.CreateGame("Just a simple | game of c&c");
				((IrcClient)client).Game.OpponentName = "Hannes";

				client.SendRandomSeed(234);

				// test the sending of a deck
				Deck deck = CreateDeck();
				//client.SendDeck(deck);

				Console.WriteLine("Press any key to quit.");
				Console.ReadKey();
			}
			finally
			{
				client.Disconnect();
			}
		}

		static void client_JoinResponse(Game game, bool accepted, string message)
		{
			if (accepted)
				Console.WriteLine("Join request {0} was accepted!", game.ID);
			else
				Console.WriteLine("Join request {0} was not accepted: {1}", game.ID, message);
		}

		static void client_JoinRequest(Game game, string playerName)
		{
			Console.WriteLine("A request was made by {0} to join {1}.", playerName, game.ID);
			client.AcceptJoin(game.ID, playerName, null);
		}

		static void client_DeckReceived(Deck deck)
		{
			Console.WriteLine("DECK RECEIVED:");
			foreach (Card card in deck)
			{
				Console.WriteLine("{0} - {1}", card.CardName, card.ID);
			}
		}

		private static Deck CreateDeck()
		{
			Deck result = new Deck();

			for (int i = 0; i < 60; i++)
			{
				// create the card
				Card card = new Card();
				card.ID = i;
				card.CardName = string.Concat("SG1", i.ToString().PadLeft(2, '0'));

				// add the card to the deck
				result.Add(card);
			}

			return result;
		}

		static void client_OpenGameReceived(Game game)
		{
			Console.WriteLine("Open Game: {0} by {1} - {2} ", game.ID, game.OpponentName, game.Description);

			// request to join the game
			client.JoinGame(game.ID);
		}

		private static void client_RandomSeedReceived(int seed)
		{
			Console.WriteLine("Random Seed: {0}", seed);
		}
	}
}
